if (typeof inweo.core.event == "undefined") {
	inweo.set("inweo.core.event", {
		id: 1000,
		started: [],
		listeners: [],
		mouse: ["mousedown", "mouseup", "mousepress", "click", "dblclick", "mousemove", "mouseout", "mouseover"],
		keyboard: ["keydown", "keyup", "keypress"]
	});
	inweo.core.event = new Class({
		type: null,
		addEvent: function(type, callback) {
			this.type = type;
			if (this.widget.getTag() == "html") {
				return;
			}
			this.startListener(type);
			if (this.global.keyboard.contains(type)) {
				this.focusable();
			}
			this.global.id++
			this.id = this.global.id;
			if (!this.global.listeners[type]) {
				this.global.listeners[type] = [];
			}
			this.global.listeners[type].push({
				"id": this.id,
				"widget": this.widget,
				"callback": callback
			});
			return this.id;
		},
		executeKeyboardCallbacks: function(event, callbacks) {
			var self = this;
			callbacks.each(function(callback) {
				if (callback.widget.hasClass("widgetFocused") == false) {
					return;
				}
				event.callback = callback;
				callback.callback.call(self, event);
			});
		},
		executeMouseCallbacks: function(event, callbacks) {
			var self = this;
			callbacks.each(function(callback) {
				if ((callback.widget.hasChild(event.target) == true) || (callback.widget == event.target)) {
					event.callback = callback;
					callback.callback.call(self, event);
				}
			});
		},
		focusable: function() {
			var self = this;
			this.widget.addClass("widgetFocusAble");
			new inweo.core.event(this.widget).addEvent("mousedown", function(event) {
				document.getElements(".widgetFocused").each(function(element) {
					element.removeClass("widgetFocused");
				});
				self.widget.addClass("widgetFocused");
				self.widget.setProperty("focusEventId", event.callback.id);
			});
		},
		initialize: function(widget) {
			this.widget = null;
			this.global = inweo.get("inweo.core.event");
			if ($type(widget) == "element") {
				this.widget = widget;
			} else if ($type(widget.widget) == "element") {
				this.widget = widget.widget;
			}
		},
		removeEventByType: function(type, id) {
			
		},
		removeEvent: function(event) {
			var self = this;
			if (this.global.keyboard.contains(event.type)) {
				this.global.listeners["mousedown"].each(function(callback, i) {
					try {
						if (callback.id.toInt() == event.callback.widget.getProperty("focusEventId").toInt()) {
							event.callback.widget.removeProperty("focusEventId");
							event.callback.widget.removeClass("widgetFocused");
							self.global.listeners["mousedown"].splice(i);
						}
					} catch(e) {}
				});
			}
			this.global.listeners[event.type].each(function(callback, i) {
				if (callback.id.toInt() == event.id.toInt()) {
					self.global.listeners[event.type].splice(i);
				}
			});
		},
		startListener: function(type) {
			var self = this;
			if (this.global.started[type] == true) {
				return true;
			}
			this.global.started[type] = true;
			$$("html")[0].addEvent(type, function(event) {
				if (self.global.mouse.contains(type)) {
					return self.executeMouseCallbacks(event, self.global.listeners[type], type);
				} else if (self.global.keyboard.contains(type)) {
					return self.executeKeyboardCallbacks(event, self.global.listeners[type], type);
				}
			});
		}
	});
}